<template>
    <view class="my-settle-container" v-if="total !== 0">
        <!-- 全选 -->
        <label class="radio" @click="changAllState">
            <radio color="#c00000" :checked="ifFullCheck"><text>全选</text></radio>
        </label>
        <!-- 合计 -->
        <view class="amount-box">
            合计: <text class="amount">{{checkedGoodsAmount}} 元</text>
        </view>
        <!-- 结算 -->
        <view class="btn-settle" @click="settlement">结算 ({{checkedCount}}) </view>
    </view>
    <!-- 无货 -->
    <view class="empty-cart" v-else>
        <image src="/static/tab_icons/cart.png" class="empty-img"></image>
        <text class="tip-text">空空如也~</text>
    </view>
</template>

<script>
    import {
        mapGetters,
        mapMutations,
        mapState
    } from 'vuex'

    export default {
        name: "my-settle",
        computed: {
            ...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
            ...mapGetters('m_user', ['addstr']),
            ...mapState('m_user', ['token']),
            ...mapState('m_cart', ['cart']),
            ifFullCheck() {
                return this.total === this.checkedCount
            },
            payGoods(){
                // 1. 过滤选中
                // 2. 遍历 调整对象 需要的属性
                return this.cart.filter(e => e.goods_state).map(e => ({
                        goods_id: e.goods_id,
                        goods_number: e.goods_count,
                        goods_price: e.goods_price
                    }))
            }
        },
        data() {
            return {
                seconds: 3,
                // 定时器的 Id
                timer: null
            };
        },
        methods: {
            ...mapMutations('m_cart', ['updateAllGoodsState']),
            ...mapMutations('m_user', ['updateRedirectInfo']),
            changAllState() {
                this.updateAllGoodsState(!this.ifFullCheck)
            },
            // 结算按钮事件
            settlement() {
                // 1. 判断是否选有商品
                if (!this.checkedCount) return uni.$showMsg('请选择要结算的商品')
                // 2. 判断是否选有收货地址
                if (!this.addstr) return uni.$showMsg('请选择收货地址')

                // 3. 判断是否登录用户
                // 如果没有登录，则调用 delayNavigate() 进行倒计时的导航跳转
                // if (!this.token) return uni.$showMsg('请先登录账号')
                if (!this.token) return this.delayNavigate()
                
                // 支付功能
                this.payOrder()
            },
            async payOrder(){
                // 1. 创建订单
                // 1.1 必要参数 order_price价格 , consignee_addr地址 , goods商品明细
                const orderInfo = {
                    order_price: 1,
                    consignee_addr: this.addstr,
                    goods: this.payGoods
                }
                // 1.2 请求创建订单
                const {data:res} = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
                console.log(res);
                if(res.meta.status != 200) return uni.$showMsg('创建订单失败!')
                // 获取订单号
                const orderNumber = res.message.order_number
                
                // 2. 订单预支付
                // 2.1 发起订单支付消息
                const {data:res2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',
                    { order_number: orderNumber })
                // 2.2 预付订单生成
                if(res2.meta.status !== 200) return uni.$showMsg('预付订单生成失败')
                // 2.3 得到订单支付相关的必要参数
                const payInfo = res2.message.pay
                
                // 3. 发起微信支付
                // 3.1 调用 requestPayment() 发起微信支付
                const zf = await uni.requestPayment(payInfo)
                // 3.2 完成未支付 (判断是否成功)
                if (err) return uni.$showMsg('订单未支付！')
                // 3.3 支付请求
                const { data: res3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', 
                    { order_number: orderNumber })
                // 3.4 检测订单
                if(res3.meta.status !== 200) return uni.$showMsg('订单为支付!')
                uni.showToast({
                    title:'完成支付',
                    icon:'success'
                })
                
            },
            // 延迟导航 my页面
            delayNavigate() {
                this.seconds = 3
                
                // 定时器
                this.showTips(this.seconds)
                this.timer = setInterval(() => {
                    this.seconds--
                    
                    if(this.seconds <= 0){
                        clearInterval(this.timer)
                        uni.switchTab({
                            url:'/pages/my/my',
                            success:()=>{
                                this.updateRedirectInfo({
                                    openType:'switchTab',
                                    from: '/pages/cart/cart'
                                })
                            }
                        })
                        return
                    }
                    
                    this.showTips(this.seconds)
                }, 1000)
            },
            // 展示倒计时提示
            showTips(n) {
                uni.showToast({
                    icon: 'none',
                    title: '请重新登录再进行结算!' + n + '秒后自动跳转!',
                    mask: true,
                    duration: 1500
                })
            },
        }
    }
</script>

<style lang="scss">
    .my-settle-container {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 50px;
        background-color: white;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 5px;
        font-size: 14px;

        .radio {
            display: flex;
            align-items: center;
        }

        .amount {
            color: #c00000;
        }

        .btn-settle {
            height: 50px;
            min-width: 100px;
            background-color: #c00000;
            color: white;
            line-height: 50px;
            text-align: center;
            padding: 0 10px;
        }
    }

    .empty-cart {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding-top: 150px;

        .empty-img {
            width: 90px;
            height: 90px;
        }

        .tip-text {
            font-size: 12px;
            color: gray;
            margin-top: 15px;
        }
    }
</style>
